<template>
  <el-card style="height: auto;">
    <el-card style="height: 50px;width: 100%">
      <el-button type="primary" icon="el-icon-arrow-left" size="small" v-on:click="return_()"
                 style="float:left;margin-top: -0.5%;">返回
      </el-button>
      <el-breadcrumb separator-class="el-icon-arrow-right" style="margin-left: 2%;float: left">
        <el-breadcrumb-item>资源管理与分析</el-breadcrumb-item>
        <el-breadcrumb-item :to="{path:'/app/manager'}">App管理</el-breadcrumb-item>
        <el-breadcrumb-item>App分析</el-breadcrumb-item>
      </el-breadcrumb>
    </el-card>
    <div style="width: 100%;height: 260px ;margin-top: 2%">
      <el-card style="height: 100%;width: 35%;float: left">
        <div class="block">
          <el-avatar shape="square" :size="150" :src="squareUrl" style="float: left"></el-avatar>
          <div style="float: left;width: 60%;margin-left: 5%">
            <h1 style="margin-top: 5%">{{ app.appName }}</h1>
            <el-divider/>
            <h4>共记录行为数据：{{ total }} 条</h4>
            <h4 style="margin-top: 5%">共注册群组：{{ groupCount }} 个</h4>
          </div>
        </div>
        <el-divider/>
        <h4>注册时间：{{ app.createTime }}</h4>
      </el-card>
      <div ref="head_total" style="height: 120%;width: 58%;float: left;margin-left: 5%"></div>
    </div>
    <el-card style="height: auto;width: 100%">
      <div ref="time_total" style="width: 70%;float: left;height: 500px;margin-left: -5%"></div>
      <el-card style="float: left;width: 25%;height: 500px;margin-left:3%">
        <el-table
          :data="groups"
          border
          height="400px"
          style="width: 100%">
          <el-table-column
            prop="groupName"
            label="群组名称"
            width="180">
          </el-table-column>
          <el-table-column prop="" label="分析">
            <template slot-scope="scope">
              <el-button size="mini" @click="analys_group(scope.$index, scope.row)">查看分析</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="block" style="margin-top: 8%">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="10"
            layout="total, sizes, prev, pager, next"
            :total="this.totalElements">
          </el-pagination>
        </div>
      </el-card>
    </el-card>
    <el-card style="height: auto;width: 100%;margin-top: 3%">
      <div style="height: auto;width: 35%;float: left">
        <div ref="verb_type" style="height: 500px;width: 100%"></div>
        <div ref="object_type" style="height: 500px;width: 100%"></div>
      </div>
      <div style="height: 700px;width: 60%;float: left;margin-left: 5%">
        <div ref="verb_top5" style="height: 500px;width: 100%"></div>
        <div ref="object_top5" style="height: 500px;width: 100%"></div>
      </div>
    </el-card>
  </el-card>
</template>
<script>
  export default {
    mounted: function () {
      this.getApp()
      this.getGroup()
      this.getType()
      this.initHeadTotal()
      this.initVerbTopFive()
      this.initObjectTopFive()
      this.initTimeTotal()
    },
    data() {
      return {
        app: {},
        total: 0,
        groupCount: 0,
        squareUrl: '',
        groups: [],
        currentPage: 1,
        pageSize: 10,
        totalElements: 0,
        head_total_data: {},
        verbType: {},
        objectType: {},
        verbTop5: {},
        objTop5: {},
        count:{}
      }
    },
    methods: {
      initHeadTotal() {
        let app = this;
        let url = "/app/behavior/fiveDayCount"
        this.$axios.get(url, {
          params: {
            appId: this.$route.query.id
          }
        }).then(successResponse => {
          this.head_total_data = successResponse.data

          let myChart = app.$echarts.init(this.$refs.head_total);
          var option;

          var posList = [
            'left', 'right', 'top', 'bottom',
            'inside',
            'insideTop', 'insideLeft', 'insideRight', 'insideBottom',
            'insideTopLeft', 'insideTopRight', 'insideBottomLeft', 'insideBottomRight'
          ];

          app.configParameters = {
            rotate: {
              min: -90,
              max: 90
            },
            align: {
              options: {
                left: 'left',
                center: 'center',
                right: 'right'
              }
            },
            verticalAlign: {
              options: {
                top: 'top',
                middle: 'middle',
                bottom: 'bottom'
              }
            },
            position: {
              options: posList.reduce(function (map, pos) {
                map[pos] = pos;
                return map;
              }, {})
            },
            distance: {
              min: 0,
              max: 100
            }
          };

          app.config = {
            rotate: 90,
            align: 'left',
            verticalAlign: 'middle',
            position: 'insideBottom',
            distance: 15,
            onChange: function () {
              var labelOption = {
                normal: {
                  rotate: app.config.rotate,
                  align: app.config.align,
                  verticalAlign: app.config.verticalAlign,
                  position: app.config.position,
                  distance: app.config.distance
                }
              };
              myChart.setOption({
                series: [{
                  label: labelOption
                }, {
                  label: labelOption
                }, {
                  label: labelOption
                }, {
                  label: labelOption
                }]
              });
            }
          };
          var labelOption = {
            show: true,
            position: app.config.position,
            distance: app.config.distance,
            align: app.config.align,
            verticalAlign: app.config.verticalAlign,
            rotate: app.config.rotate,
            formatter: '{c}  {name|{a}}',
            fontSize: 10,
            rich: {
              name: {}
            }
          };

          option = {
            tooltip: {
              trigger: 'axis',
              axisPointer: {
                type: 'shadow'
              }
            },
            title: {
              show: true,
              text: '每日类型统计',

              left: 'center'
            },
            legend: {
              orient: 'vertical',
              left: 'right',
              top: 'middle',
              align: 'right',
              data: ['agent', 'group', 'verb', 'object']
            },
            xAxis: [
              {
                type: 'category',
                axisTick: {show: false},
                data: this.head_total_data.date
              }
            ],
            yAxis: [
              {
                type: 'value'
              }
            ],
            series: [
              {
                name: 'agent',
                type: 'bar',
                barGap: 0,
                emphasis: {
                  focus: 'series'
                },
                data: this.head_total_data.agentData
              },
              {
                name: 'group',
                type: 'bar',
                emphasis: {
                  focus: 'series'
                },
                data: this.head_total_data.groupData
              },
              {
                name: 'verb',
                type: 'bar',
                emphasis: {
                  focus: 'series'
                },
                data: this.head_total_data.verbData
              },
              {
                name: 'object',
                type: 'bar',
                emphasis: {
                  focus: 'series'
                },
                data: this.head_total_data.objectData
              }
            ]
          };

          option && myChart.setOption(option);

        }).catch(failResponse => {
        })
      },
      initVerbType() {
        let app = this;
        let myChart = app.$echarts.init(this.$refs.verb_type);
        var option;
        option = {
          title: {
            left: 'center',
            text: 'verb类型统计'
          },
          radar: {
            // shape: 'circle',

            indicator: this.verbType.indicator
          },
          series: [{
            name: 'VERB',
            type: 'radar',
            data: [
              {
                value: this.verbType.value,
                name: 'VERB'
              }
            ]
          }]
        };

        option && myChart.setOption(option);
      },
      initObjectType() {
        let app = this;
        let myChart = app.$echarts.init(this.$refs.object_type);
        var option;
        option = {
          title: {
            left: 'center',
            text: 'object类型统计'
          },
          radar: {
            // shape: 'circle',
            indicator: this.objectType.indicator
          },
          series: [{
            name: 'Object',
            type: 'radar',
            data: [
              {
                value: this.objectType.value,
                name: 'Object'
              }
            ]
          }]
        };

        option && myChart.setOption(option);
      },
      initVerbTopFive() {
        let app = this;
        let url = "/app/behavior/verbTop"
        this.$axios.get(url, {
          params: {
            appId: this.$route.query.id
          }
        }).then(successResponse => {
          this.verbTop5 = successResponse.data
          let myChart = app.$echarts.init(this.$refs.verb_top5);
          var option;

          var labelRight = {
            position: 'right'
          };
          option = {
            tooltip: {
              trigger: 'axis',
              axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
              }
            },
            grid: {
              top: 80,
              bottom: 30
            },
            xAxis: {
              type: 'value',
              position: 'top',
              splitLine: {
                lineStyle: {
                  type: 'dashed'
                }
              }
            },
            yAxis: {
              type: 'category',
              axisLine: {show: false},
              axisLabel: {show: false},
              axisTick: {show: false},
              splitLine: {show: false},
              data: this.verbTop5.verbs
            },
            series: [
              {
                name: '数量',
                type: 'bar',
                stack: '总量',
                label: {
                  show: true,
                  formatter: '{b}'
                },
                data: this.verbTop5.data
              }
            ]
          };
          option && myChart.setOption(option);
        }).catch(failResponse => {
        })
      },
      initObjectTopFive() {

        let app = this;
        let myChart = app.$echarts.init(this.$refs.object_top5);
        var option;
        let url = "/app/behavior/objTop"
        this.$axios.get(url, {
          params: {
            appId: this.$route.query.id
          }
        }).then(successResponse => {
          this.objTop5 = successResponse.data
          var labelRight = {
            position: 'right'
          };
          option = {
            tooltip: {
              trigger: 'axis',
              axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
              }
            },
            grid: {
              top: 80,
              bottom: 30
            },
            xAxis: {
              type: 'value',
              position: 'top',
              splitLine: {
                lineStyle: {
                  type: 'dashed'
                }
              }
            },
            yAxis: {
              type: 'category',
              axisLine: {show: false},
              axisLabel: {show: false},
              axisTick: {show: false},
              splitLine: {show: false},
              data: this.objTop5.objs
            },
            series: [
              {
                name: '数量',
                type: 'bar',
                stack: '总量',
                label: {
                  show: true,
                  formatter: '{b}'
                },
                data: this.objTop5.data
              }
            ]
          };
          option && myChart.setOption(option);
        }).catch(failResponse => {
        })
      },
      initTimeTotal() {
        let app = this;
        let myChart = app.$echarts.init(this.$refs.time_total);
        var option;
        let url = "/app/behavior/count"
        this.$axios.get(url, {
          params: {
            appId: this.$route.query.id
          }
        }).then(successResponse => {
            this.count = successResponse.data
        option = {
          title: {
            text: '昨日行为数据总数分布',
            left: 'center'
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'cross'
            }
          },
          xAxis: {
            type: 'category',
            boundaryGap: false,
            data: this.count.times
          },
          yAxis: {
            type: 'value',
            axisLabel: {
              formatter: '{value}'
            },
            axisPointer: {
              snap: true
            }
          },
          series: [
            {
              type: 'line',
              smooth: true,
              data: this.count.data,
              markArea: {
                itemStyle: {
                  color: 'rgba(255, 173, 177, 0.4)'
                },
                data: []
              }
            }
          ]
        };

        option && myChart.setOption(option);
        }).catch(failResponse => {
        })
      },

      handleSizeChange(val) {
        this.pageSize = val;
        this.currentPage = 0
        this.getGroup()
      },
      handleCurrentChange(val) {
        this.currentPage = val
        this.getGroup()
      },
      getGroup() {

        let that = this
        let url = "/api/group/all"
        this.$axios.get(url, {
          params: {
            appId: this.$route.query.id,
            currentPage: this.currentPage - 1,
            pageSize: this.pageSize
          }
        }).then(successResponse => {

          that.groups = successResponse.data.content
          console.log(that.groups)

        }).catch(failResponse => {
        })
      },
      getType() {

        let that = this
        let url = "/app/behavior/verbType"
        this.$axios.get(url, {
          params: {
            appId: this.$route.query.id,
          }
        }).then(successResponse => {
          this.verbType = successResponse.data.verb
          this.objectType = successResponse.data.object
          this.initVerbType()
          this.initObjectType()

        }).catch(failResponse => {
        })
      },
      getApp() {
        let that = this
        let url = "/api/app/show/" + this.$route.query.id
        this.$axios.get(url).then(successResponse => {
          that.app = successResponse.data
          that.app.createTime = that.datefilter(that.app.createTime, "yyy-mm-dd")
          that.groupCount = that.app.entity12Groups.length - 1
        }).catch(failResponse => {
        })
        url = "/app/behavior/total"
        this.$axios.get(url, {
          params: {
            appId: this.$route.query.id
          }
        }).then(successResponse => {
          that.total = successResponse.data
        }).catch(failResponse => {
        })
      },
      analys_group(index, row) {
        this.$router.push({path: '/group/analyse', query: {id: row.groupId}})
      },
      return_() {
        this.$router.go(-1);
      },
      datefilter: function (value, args) {
        var dt = new Date(value)

        var y = dt.getFullYear()
        //这里month得加1
        var m = dt.getMonth() + 1
        var d = dt.getDate()
        //如果要求的时间格式只有年月日
        if (args.toLowerCase() === "yyy-mm-dd") {
          return `${y}-${m}-${d}`
          //如果时间要求精确到时分秒
        } else {
          var hh = dt.getHours()
          var mm = dt.getMinutes()
          var ss = dt.getSeconds();
          return `${y}-${m}-${d}:${hh}:${mm}:${ss}`
        }
      }
    }
  }
</script>
<style>
  .block {
    height: 150px;
  }
</style>
